package com.learnkitchen.learnkitchenrecipes.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.learnkitchen.learnkitchenrecipes.ProgressFragment;


/**
 * Created by Administrator on 2018-01-03.
 */

public abstract class BaseFragment extends Fragment {

    private ProgressFragment progressFragment;
    private Context mContext;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getLayout(), container, false);
        initView(view);
        return view;
    }

    protected abstract int getLayout();

    protected abstract void initView(View v);

    /**
     * 有些机型是上下为空的情况比如华为P8青春版
     * @param msg
     */
    public void showToast(String msg) {
        if(mContext != null){
            Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 显示等待对话框
     */
    public void showLoading(String textString) {
        // 显示进度对话框
        progressFragment = new ProgressFragment();
        Bundle bundle = new Bundle();
        bundle.putString("loading", textString);
        progressFragment.setArguments(bundle);
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.add(progressFragment, "progressFragment");
        ft.commitAllowingStateLoss();
    }

    /**
     * 关闭进度对话框
     */
    public void hideLoading() {
        // 关闭进度对话框
        if (progressFragment != null) {
            progressFragment.dismissAllowingStateLoss();
        }
    }

    /**
     * 刷新数据，此方法必须要在isRegisterEventBus return true时才回调用
     *
     * @param data
     */
    protected void refreshUI(int eventId,Object data) {


    }

}
